package com.example.androidtvdemo.demo04;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.leanback.widget.FocusHighlight;
import androidx.leanback.widget.FocusHighlightHelper;
import androidx.leanback.widget.ItemBridgeAdapter;

import com.example.androidtvdemo.BuildConfig;
import com.example.androidtvdemo.databinding.ActivityDemo04Binding;
import com.example.androidtvdemo.net.BlogArticle;
import com.example.androidtvdemo.net.HttpManager;
import com.example.androidtvdemo.net.IappService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Demo04Activity extends FragmentActivity {

    private ActivityDemo04Binding mVb;
    private ArrayObjectAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mVb = ActivityDemo04Binding.inflate(getLayoutInflater());
        setContentView(mVb.getRoot());
        mVb.vgv.setNumColumns(4);
        //初始化列表需要的adapter
        mAdapter = new ArrayObjectAdapter(new BlogArticlePresenter());
        ItemBridgeAdapter iba = new ItemBridgeAdapter(mAdapter);
        mVb.vgv.setAdapter(iba);
        //选中放大效果增加这个代码,flat中有放大比例
        FocusHighlightHelper.setupBrowseItemFocusHighlight(iba, FocusHighlight.ZOOM_FACTOR_MEDIUM, false);
        //请求博客文章数据
        IappService service = HttpManager.getService(IappService.class, BuildConfig.APP_BASE_URL);
        service.getArticle().enqueue(new Callback<BlogArticle>() {
            @Override
            public void onResponse(Call<BlogArticle> call, Response<BlogArticle> response) {
                //数据获取成功之后,显示图片数据
                Log.i("->>", "onResponse: 获取数据成功");
                List<BlogArticle.DataBean.ContentBean> content = response.body().getData().getContent();
                //更新数据
                mAdapter.setItems(content, null);
            }

            @Override
            public void onFailure(Call<BlogArticle> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}